home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD 2.1 / Amiga Developer CD v2.1.iso / Reference / DevCon / Milan_1991 / Devcon91.1 / Libraries / Intuition / other_examples / ScDemo / scdemo.c next >
Encoding:
C/C++ Source or Header  |  1992-09-01  |  10.3 KB  |  468 lines

  1. /* Screen Demo Program     :ts=8 */
  2.  
  3. /* compile lc -L -iVINCLUDE: -iINCLATTICE: scdemo */
  4.  
  5. /*
  6. Copyright (c) 1989 Commodore-Amiga, Inc.
  7.  
  8. Executables based on this information may be used in software
  9. for Commodore Amiga computers. All other rights reserved.
  10. This information is provided "as is"; no warranties are made.
  11. All use is at your own risk, and no liability or responsibility
  12. is assumed.
  13. */
  14.  
  15. #include <exec/types.h>
  16. #include <exec/lists.h>
  17. #include <exec/memory.h>
  18.  
  19. #include <graphics/displayinfo.h>
  20.  
  21. #include <utility/tagitem.h>
  22. #include <intuition/intuition.h>
  23.  
  24. /* prototypes */
  25. #include <proto/all.h>
  26.  
  27. #include <stdio.h>
  28.  
  29. struct  IntuitionBase   *IntuitionBase;
  30. struct  GfxBase         *GfxBase;
  31.  
  32. struct List    ScreenList;        /* keep track of open screens    */
  33.  
  34. struct ScreenNode    {
  35.     struct MinNode    Sn_Node;    
  36.     struct Screen     *sn_Screen;
  37. };
  38.  
  39. struct Screen *addScreen();
  40. void cleanup();
  41. void freeScreens();
  42. void drawRulers();
  43.  
  44. char    kbuff[257];
  45.  
  46. void
  47. showHelp()
  48. {
  49.     printf("commands:\n");
  50.     printf("f - free screens        q - quit\n");
  51.     printf("l - lo-res              L - lo-res interlace\n");
  52.     printf("h - hi-res              H - hi-res interlace\n");
  53.     printf("p - productivity        P - productivity interlace\n");
  54.     printf("s - superhires          S - superhires interlace\n");
  55.     printf("o - std overcan hires   O - video overscan hires\n");
  56.     printf("a - A2024 10 Hz         A - A2024 15 Hz\n");
  57.     printf("w - wide (scrolling) hi res\n");
  58.     printf("z - VGA lores (70ns pixels)\n");
  59.     printf("$ - hex display mode specifier\n");
  60. }
  61.  
  62.  
  63. main()
  64. {
  65.     printf("SCDemo: accompanies V1.4 Beta 1.\n");
  66.  
  67.     NewList( &ScreenList );
  68.  
  69.     if (!(IntuitionBase = 
  70.     (struct IntuitionBase *) OpenLibrary("intuition.library", 36L)))
  71.     { cleanup("no V36 intuition library\n"); }
  72.  
  73.     if (!(GfxBase = (struct GfxBase *) OpenLibrary("graphics.library", 36L)))
  74.     { cleanup("no V36 graphics library\n"); }
  75.  
  76.     FOREVER
  77.     {
  78.     printf("screen command (? for list): ");
  79.     if (!gets(kbuff)) cleanup("eof.\n");
  80.  
  81.     switch ( *kbuff )
  82.     {
  83.     case 'o':
  84.         addScreen( 0xf00, 0x555,
  85.             SA_DisplayID,    HIRES_KEY,
  86.         SA_Overscan,    OSCAN_STANDARD,
  87.         SA_Title,    " Hires Standard Overscan ",
  88.         TAG_END );
  89.         break;
  90.  
  91.     case 'O':
  92.         addScreen( 0xf00, 0x555,
  93.             SA_DisplayID,    HIRES_KEY,
  94.         SA_Overscan,    OSCAN_VIDEO,
  95.         SA_Title,    " Hires Video Overscan ",
  96.         TAG_END );
  97.         break;
  98.  
  99.     case 'a':
  100.         addScreen( 0xfff, 0x000,
  101.             SA_DisplayID,    A2024TENHERTZ_KEY,
  102.         SA_Title,    " A2024, 10 Hz ",
  103.         TAG_END );
  104.         break;
  105.  
  106.     case 'A':
  107.         addScreen( 0x000, 0xfff,
  108.             SA_DisplayID,    A2024FIFTEENHERTZ_KEY,
  109.         SA_Title,    " A2024, 15 Hz ",
  110.         TAG_END );
  111.         break;
  112.  
  113.     case 's':
  114.         addScreen( 0x005, 0xff0,
  115.             SA_DisplayID,    SUPER_KEY,
  116.         SA_Title,    " Super-Hires, Non-interlaced ",
  117.         TAG_END );
  118.         break;
  119.  
  120.     case 'S':
  121.         addScreen( 0x005, 0xff0,
  122.             SA_DisplayID,    SUPERLACE_KEY,
  123.         SA_Title,    " Super-Hires, Interlaced ",
  124.         TAG_END );
  125.         break;
  126.  
  127.     case 'p':
  128.         addScreen( 0xfff, 0x000,
  129.             SA_DisplayID,    VGAPRODUCT_KEY,
  130.         SA_Title,    " Productivity Mode ",
  131.         TAG_END );
  132.         break;
  133.  
  134.     case 'P':    /* productivity, lace    */
  135.         addScreen( 0x005, 0xf00,
  136.             SA_DisplayID,    VGAPRODUCTLACE_KEY,
  137.         SA_Title,    " Productivity, Interlaced ",
  138.         TAG_END );
  139.         break;
  140.  
  141.     case 'z':
  142.         addScreen( 0xfff, 0x000,
  143.             SA_DisplayID,    VGALORES_KEY,
  144.         SA_Title,    " 70ns pixels, Doublescan ",
  145.         TAG_END );
  146.         break;
  147.  
  148.     case 'h':
  149.         addScreen( 0x0f0, 0x555,
  150.             SA_DisplayID,    HIRES_KEY,
  151.         SA_Title,    " HiRes Text Overscan ",
  152.         TAG_END );
  153.         break;
  154.  
  155.     case 'H':
  156.         addScreen( 0xf00, 0x555,
  157.             SA_DisplayID,    HIRESLACE_KEY,
  158.         SA_Title,    " HiRes Interlaced ",
  159.         TAG_END );
  160.         break;
  161.  
  162.     case 'L':
  163.         addScreen( 0x0ff, 0x555,
  164.             SA_DisplayID,    LORESLACE_KEY,
  165.         SA_Title,    " LoRes Lace Screen ",
  166.         TAG_END );
  167.         break;
  168.  
  169.     case 'l':
  170.         addScreen( 0x00f, 0x555,
  171.             SA_DisplayID,    LORES_KEY,
  172.         SA_Title,    " LoRes Screen ",
  173.         TAG_END );
  174.         break;
  175.  
  176.      case 'w':
  177.         addScreen( 0xf0f, 0x555,
  178.             SA_DisplayID,    HIRES_KEY,
  179.         SA_Title,    " Wide Scrolling HiRes Screen ",
  180.         /* specify a DisplayClip region to scroll around
  181.          * in.  This is a reasonable region to use.
  182.          */
  183.         SA_Overscan,    OSCAN_TEXT,
  184.         SA_Width,    1100,
  185.         SA_AutoScroll,    TRUE,    /* not working in beta 1? */
  186.         TAG_END );
  187.         break;
  188.  
  189.     case 'f':
  190.         freeScreens();
  191.         break;
  192.  
  193.     case '?':
  194.         showHelp();
  195.         break;
  196.  
  197.     case 'q':
  198.     case 'Q':
  199.         cleanup("bye.\n");
  200.         break;
  201.     case '$':
  202.         {
  203.             long    scanmode;
  204.         int    scancount;
  205.  
  206.         /* skip '$' and get long hex value */
  207.         scancount = sscanf( kbuff + 1, "%lx", &scanmode );
  208.         if ( scancount == 1 )
  209.         {
  210.             sprintf( kbuff,"Direct Mode Specification: %lx",scanmode);
  211.             addScreen( 0x0ff, 0x555,
  212.                 SA_DisplayID,    scanmode,
  213.             TAG_END );
  214.             break;
  215.         }
  216.         else
  217.         {
  218.             printf("couldn't parse: type '$ <hex num>'\n");
  219.         }
  220.         }
  221.         break;
  222.     default:
  223.         printf("don't know (%lx)\n", *kbuff, *kbuff);
  224.     }
  225.     }
  226. }
  227.  
  228. void
  229. cleanup( str )
  230. char    *str;
  231. {
  232.     if (str) printf(str);
  233.  
  234.     freeScreens();
  235.  
  236.     if (IntuitionBase) CloseLibrary(IntuitionBase);
  237.     if (GfxBase) CloseLibrary(GfxBase);
  238.  
  239.     exit (0);
  240.     return;
  241. }
  242.  
  243. /* free all screens    */
  244. void
  245. freeScreens()
  246. {
  247.     struct ScreenNode *sn;
  248.  
  249.     while (sn = (struct ScreenNode *) RemHead( &ScreenList ))
  250.     {
  251.     CloseWindow( sn->sn_Screen->FirstWindow );
  252.     printf("freeing screen at %lx.\n", sn->sn_Screen );
  253.     CloseScreen( sn->sn_Screen );
  254.     FreeMem( sn, (long) sizeof *sn );
  255.     }
  256.     return;
  257. }
  258.  
  259. char    *string = "Test String A B b CdEf m M n N o d D";
  260.  
  261. struct ColorSpec colorspec[] = {
  262.     { 0 },
  263.     { 1 },
  264.     {-1 }
  265. };
  266.  
  267. #define REDPART( color )    (( (color) >> 8 ) & 0xf )
  268. #define GREENPART( color )    (( (color) >> 4 ) & 0xf )
  269. #define BLUEPART( color )    (  (color) & 0xf )
  270.  
  271. struct Screen *
  272. addScreen( color0, color1, tags )
  273. int    color0, color1;
  274. ULONG    tags;
  275. {
  276.     struct ScreenNode *sn;
  277.     struct Screen    *OpenScreenTags();
  278.     struct Window    *OpenWindowTags();
  279.     struct Window    *window;
  280.     ULONG    errorcode;    /* for OpenScreen() errors    */
  281.  
  282.     if (!(sn = (struct ScreenNode *)
  283.           AllocMem((LONG)sizeof (struct ScreenNode), 0L )) )
  284.         return (NULL);
  285.  
  286.     colorspec[0].Red = REDPART( color0 );
  287.     colorspec[0].Green = GREENPART( color0 );
  288.     colorspec[0].Blue = BLUEPART( color0 );
  289.  
  290.     colorspec[1].Red = REDPART( color1 );
  291.     colorspec[1].Green = GREENPART( color1 );
  292.     colorspec[1].Blue = BLUEPART( color1 );
  293.  
  294.     sn->sn_Screen =  OpenScreenTags( NULL,
  295.                 SA_Colors,    colorspec,
  296.             SA_ErrorCode,    &errorcode,
  297.             TAG_MORE,    &tags,    /* jump to varargs list */
  298.             TAG_END );
  299.  
  300.     if ( sn->sn_Screen )
  301.     {
  302.     window = OpenWindowTags( NULL,
  303.         WA_CustomScreen,    sn->sn_Screen,
  304.         WA_Title,         " Window in Screen ",
  305.         TAG_END );
  306.  
  307.     printf("window at %lx\n", window );
  308.  
  309.         if ( window )    /* i.e., success */
  310.         {
  311.         printf("new screen at %lx.\n", sn->sn_Screen);
  312.         AddTail(&ScreenList, (struct Node *) sn);
  313.  
  314.         /* draw a little something at the bottom    */
  315.         drawRulers( sn->sn_Screen );
  316.         return (sn->sn_Screen);
  317.         }
  318.     else
  319.     {
  320.         printf( "couldn't get window for screen\n" );
  321.         CloseScreen( sn->sn_Screen );
  322.     }
  323.     }
  324.  
  325.     else
  326.     {
  327.     printf("can't open screen: ");
  328.         switch ( errorcode )
  329.     {
  330.     case OSERR_NOMONITOR:
  331.         printf("monitor not available.\n");
  332.         break;
  333.     case OSERR_NOCHIPS:
  334.         printf("new chipset not available.\n");
  335.         break;
  336.     case OSERR_NOMEM:
  337.         printf("memory not available.\n");
  338.         break;
  339.     case OSERR_NOCHIPMEM:
  340.         printf("chip memory not available.\n");
  341.         break;
  342.     case OSERR_PUBNOTUNIQUE:
  343.         printf("public screen already open.\n");
  344.         break;
  345.     case OSERR_UNKNOWNMODE:
  346.         printf("mode ID is unknown.\n");
  347.         break;
  348.     default:
  349.         printf("unknown error %ld\n", errorcode );
  350.     }
  351.     }
  352.     FreeMem(sn, (LONG) sizeof (struct ScreenNode));
  353.     return (NULL);
  354. }
  355.  
  356. #define HRULEY        (90L)
  357. #define VRULEX        (250L)
  358. #define VRULESTART    (1)            /* start VRULE at 200    */
  359.  
  360. #define HASHHEIGHT    (20L)        /* size of hash marks    */
  361. #define HASHWIDTH    (20L)    
  362.  
  363. /*
  364.  * draw rulers in screen
  365.  */
  366. UBYTE    posbuff[ 81 ];
  367.  
  368. void
  369. drawRulers( sc )
  370. struct Screen *sc;
  371. {
  372. #if 0
  373.     struct RastPort *rp = sc->FirstWindow->RPort;
  374. #else
  375.     struct RastPort *rp = &sc->RastPort;
  376. #endif
  377.     int        hunnert;
  378.     int        pos;
  379.     int        endpos;
  380.     int        tlength;
  381.     int        width = sc->Width;
  382.     int        height = sc->Height;
  383.  
  384.     /* report total dimensions    */
  385.     sprintf( posbuff, "Left %d, Top %d, Width %d, Height %d.",
  386.         sc->LeftEdge, sc->TopEdge, sc->Width, sc->Height );
  387.     SetAPen( rp, 1L );
  388.     Move( rp, 10L, (LONG) HRULEY - 50 );
  389.     Text( rp, posbuff, (LONG) strlen( posbuff ) );
  390.  
  391.     /* draw horizontal counters and hashmarks */
  392.     SetAPen( rp, 1L );
  393.     Move( rp, 0L, (LONG) HRULEY + HASHHEIGHT / 2 );
  394.     Draw( rp, (LONG) width - 1, (LONG) HRULEY + HASHHEIGHT / 2 );
  395.  
  396.     for (hunnert = 0; (hunnert * 100) <= width; ++hunnert)
  397.     {
  398.     /* tall hashmark at X00    */
  399.     pos = hunnert * 100;
  400.     endpos = pos + 100;
  401.     SetAPen( rp, 3L );
  402.     Move( rp, (LONG) pos, HRULEY );
  403.     Draw( rp, (LONG) pos, HRULEY  + 2 * HASHHEIGHT);
  404.  
  405.     while (  (pos += 10) < endpos )
  406.     {
  407.         if ( pos > (width - 1) ) goto BREAK1;
  408.         Move( rp, (LONG) pos, HRULEY );
  409.         Draw( rp, (LONG) pos, HRULEY  + HASHHEIGHT);
  410.     }
  411.  
  412.     sprintf( posbuff, "%d", endpos);
  413.     tlength = TextLength( rp, posbuff, (LONG) strlen( posbuff ) );
  414.     SetAPen( rp, 7L );
  415.     Move( rp, (LONG) endpos - tlength - 1,
  416.     HRULEY  + 2 * HASHHEIGHT);
  417.     Text( rp, posbuff, (LONG) strlen( posbuff ) );
  418.     }
  419. BREAK1:
  420.  
  421.     /* draw vertical counters */
  422.     Move( rp, VRULEX + HASHWIDTH / 2, (LONG) VRULESTART * 100 );
  423.     Draw( rp, VRULEX + HASHWIDTH / 2, (LONG) height - 1 );
  424.  
  425.     for (hunnert = VRULESTART;
  426.     ((hunnert * 100)) <= height; ++hunnert)
  427.     {
  428.     /* wide hashmark at X00    */
  429.     pos = hunnert * 100;
  430.     endpos = pos + 100;
  431.     Move( rp, VRULEX, (LONG) pos);
  432.     Draw( rp,  VRULEX  + 2 * HASHWIDTH, (LONG) pos);
  433.  
  434.     while (  (pos += 10) < endpos )
  435.     {
  436.         if ( pos > (height - 1) ) goto BREAK2;
  437.         Move( rp, VRULEX, (LONG) pos);
  438.         Draw( rp,  VRULEX  + HASHWIDTH, (LONG) pos);
  439.     }
  440.  
  441.     sprintf( posbuff, "%d", endpos);
  442.     tlength = TextLength( rp, posbuff, (LONG) strlen( posbuff ) );
  443.     Move( rp, VRULEX - tlength - 1, (LONG) endpos - 1);
  444.     Text( rp, posbuff, (LONG) strlen( posbuff ) );
  445.     }
  446. BREAK2:
  447.     return;
  448. }
  449.  
  450. struct Window    *
  451. OpenWindowTags( nw,tags)
  452. struct NewWindow    *nw;
  453. ULONG            tags;
  454. {
  455.     struct Window    *OpenWindowTagList();
  456.  
  457.     return ( OpenWindowTagList( nw, &tags ) );
  458. }
  459. struct Screen    *
  460. OpenScreenTags( ns, tag1, data1 )
  461. struct NewScreen    *ns;
  462. ULONG            tag1;
  463. {
  464.     struct Screen    *OpenScreenTagList();
  465.  
  466.     return ( OpenScreenTagList( ns, &tag1 ) );
  467. }
  468.